#!/bin/bash
tmp_root_dir=$PWD
echo "========================================================"
echo -e "      \e[41mOne Key Update MicroPython & ESP-IDF Project\e[0m"
echo "========================================================"

sudo apt-get install -y make

echo ""
echo -e "\e[32mUpdating ESP-IDF project...\e[0m"
cd esp-idf
git pull
source ~/.profile
cd $tmp_root_dir

echo ""
echo -e "\e[32mUpdating Micropython project...\e[0m"
cd micropython
git fetch --all && git reset --hard origin/master && git pull && git submodule update --init --recursive
# rm -rf ports/esp32/build-*
cd $tmp_root_dir

while read line
do
    tmp_line=`echo ${line} | sed s/[[:space:]]//g`

    if [[ $tmp_line =~ 'ESPIDF_SUPHASH_V3:=' ]] ; then
        sup_idf_hash=${tmp_line#*ESPIDF_SUPHASH_V3:=}
        break
    fi
done  < micropython/ports/esp32/Makefile

echo "-- new sup_idf_hash: $sup_idf_hash";
echo ""
echo -e "\e[32mCheckout IDF project branch...\e[0m"
cd esp-idf
git checkout $sup_idf_hash && git submodule update --init --recursive
cd $tmp_root_dir

echo ""
echo -e "\e[32mCleaning up...\e[0m"
cd micropython/mpy-cross && make clean && cd $tmp_root_dir
cd micropython/ports/esp32 && make clean

echo ""
echo -e "\e[32mDone\e[0m"
